/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.rational.parser.type.standart;

import org.rational.parser.type.wraps.TrimType;

/**
 *
 * @author Overmind
 */
public class Textual
    extends TrimType<String>
{
    public static final String QUOTE = "\"";
    public static final String SUB_QUOTE = "\\\"";
    
    public Textual()
    {
        super("Textual");
    }

    @Override
    public String valueOfTrimString(String string) throws NumberFormatException
    {
        if ((string.length() >= (QUOTE.length() * 2))
                && (string.startsWith(QUOTE))
                && (string.endsWith(QUOTE)))
        {
            String substring = string.substring(QUOTE.length(), string.length() - QUOTE.length());
            
            if(substring.contains(QUOTE))
            {
                throw new NumberFormatException();
            }
            
            String result = substring.replaceAll(SUB_QUOTE, QUOTE);
            return result;
        } 
        else
        {
            throw new NumberFormatException();
        }
    }

    @Override
    public String objectToString(String value)
    {
        return "\"" + value + "\"";
    }
}
